package com.ruoyi.web.controller.module.attendance;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.attendance.AttendanceGroupUser;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.wx.IWxUserService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.attendance.IAttendanceGroupUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 考勤组 成员Controller
 *
 * @author ruoyi
 * @date 2022-12-07
 */
@Validated
@Api(value = "考勤组 成员控制器", tags = {"考勤组 成员管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/attendance/groupUser")
public class AttendanceGroupUserController extends BaseController {

    private final IAttendanceGroupUserService attendanceGroupUserService;
    private final IWxUserService wxUserService;
    private final ISysUserService sysUserService;
	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

    /**
     * 查询考勤组 成员列表
     */
    @ApiOperation("查询考勤组 成员列表")
    @PreAuthorize("@ss.hasPermi('module:groupUser:list')")
    @GetMapping("/list")
    public TableDataInfo<AttendanceGroupUser> list(@Validated AttendanceGroupUser attendanceGroupUser) {
        QueryWrapper<AttendanceGroupUser> qw = new QueryWrapper<>();
        if(attendanceGroupUser.getGroupId()!=null){
            qw.eq("group_id",attendanceGroupUser.getGroupId());
        }
        if (attendanceGroupUser.getPropertyId()!=null){
            qw.eq("property_id",attendanceGroupUser.getPropertyId());
        }
        TableDataInfo<AttendanceGroupUser> td = PageUtils.buildDataInfo(attendanceGroupUserService.page(PageUtils.buildPage(),qw));
        List<AttendanceGroupUser> list = td.getRows();
        for (AttendanceGroupUser list1:list){
            if (list1.getUserId()!=null&&list1.getUserId()!=0){
                WxUser wxUser=wxUserService.findById(list1.getUserId());
                if (wxUser!=null){
					if(StrUtil.isNotEmpty(wxUser.getFaceUrl()) && !wxUser.getFaceUrl().contains("http")) {
						list1.setFaceUrl(imgUrl + wxUser.getFaceUrl());
					}else{
						list1.setFaceUrl(wxUser.getFaceUrl());
					}
                    list1.setNickName(wxUser.getNickName());
                }
            }else if (list1.getUserId()==0){
                list1.setNickName("未绑定");
            }
            if (list1.getSysUserId()!=null){
                SysUser user=sysUserService.getById(list1.getSysUserId());
                if (user!=null){
                    list1.setSysUserName(user.getUserName());
                }
            }
        }
        return td;
    }

    /**
     * 导出考勤组 成员列表
     */
    @ApiOperation("导出考勤组 成员列表")
    @PreAuthorize("@ss.hasPermi('module:groupUser:export')")
    @Log(title = "考勤组 成员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<AttendanceGroupUser> export(@Validated AttendanceGroupUser attendanceGroupUser) {
        List<AttendanceGroupUser> list = attendanceGroupUserService.list();
        ExcelUtil<AttendanceGroupUser> util = new ExcelUtil<AttendanceGroupUser>(AttendanceGroupUser.class);
        return util.exportExcel(list, "考勤组 成员");
    }

    /**
     * 获取考勤组 成员详细信息
     */
    @ApiOperation("获取考勤组 成员详细信息")
    @PreAuthorize("@ss.hasPermi('module:groupUser:query')")
    @GetMapping("/{id}")
    public AjaxResult<AttendanceGroupUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        AttendanceGroupUser attendanceGroupUser = attendanceGroupUserService.getById(id);
        return AjaxResult.success(attendanceGroupUser);
    }

    /**
     * 新增考勤组 成员
     */
    @ApiOperation("新增考勤组 成员")
    @PreAuthorize("@ss.hasPermi('module:groupUser:add')")
    @Log(title = "考勤组 成员", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceGroupUser attendanceGroupUser) {
        return toAjax(attendanceGroupUserService.save(attendanceGroupUser) ? 1 : 0);
    }

    /**
     * 修改考勤组 成员
     */
    @ApiOperation("修改考勤组 成员")
    @PreAuthorize("@ss.hasPermi('module:groupUser:edit')")
    @Log(title = "考勤组 成员", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceGroupUser attendanceGroupUser) {
        return toAjax(attendanceGroupUserService.updateById(attendanceGroupUser) ? 1 : 0);
    }

    /**
     * 删除考勤组 成员
     */
    @ApiOperation("删除考勤组 成员")
    @PreAuthorize("@ss.hasPermi('module:groupUser:remove')")
    @Log(title = "考勤组 成员" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(attendanceGroupUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
